use ${dirTemp}/compustat_quarterly_markups, clear

* Define control variables
* =============================
xtset id date_q
gen latq = log(atq)
gen lev = (dlcq+dlttq)/atq
gen liq = cheq/atq
gen Llatq = log(L.atq)
gen Llev = (L.dlcq+L.dlttq)/L.atq
gen Lliq = L.cheq/L.atq

* Within-firm variation 
foreach var of varlist Llev Lliq Llatq {
	bysort id: egen aux = mean(`var')
	gen `var'_w = `var' - aux
	drop aux
} 

* scale stickiness by cross-sectoral SD
* =============================================
egen aux1 = sd(freq_avg3) if lmarkup_PF < .
egen aux2 = sd(freq_avg3)  
gen freq_avg3_std = freq_avg3/aux1
drop aux1 

egen aux1 = sd(dur_avg3) if lmarkup_PF < .
gen dur_avg3_std = dur_avg3/aux1 
drop aux1

* Within variation for LP:  
bysort date_q naicsq2: egen aux = mean(freq_avg3_std) 
gen freq_avg3_std_w = -(freq_avg3_std - aux) // normalized sign
drop aux  

bysort date_q naicsq2: egen aux = mean(dur_avg3_std)
gen dur_avg3_std_w = dur_avg3_std - aux
drop aux   

* Add monetary shocks
* =========================== 
merge m:1 date_quarterly using ${dirTemp}/tsdata_macro, keepusing(ffr3shockTightWeighted policyShockTightWeighted ffr3shockTightAllWeighted ffr3shockTightSignWeighted ffr3shockTightNoQEWeighted ffr3shockTightGBWeiUncorr GS1) keep(3) nogen

xtset id date_q
gen DGS1 = D.GS1

* Sample period
* ==================
keep if date_quarterly >= yq(1995,1) & date_quarterly <= yq(2017,3)

* Save
* ========
save ${dirTemp}/compustat_quarterly_markups_final, replace 
